﻿using AForge.Video.DirectShow;
using AForge.Video;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace PPSMS
{
    public partial class VideoForm : UIPage
    {
        private VideoCaptureDevice videoDevice;
        private FilterInfoCollection videoDevices;
        private PictureBox pictureBoxMonitor;
        public VideoForm()
        {
            InitializeComponent();
            InitializeCamera();
        }
        private void InitializeCamera()
        {
            try
            {
                // 搜索可用的摄像头
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                if (videoDevices.Count == 0)
                {
                    throw new ApplicationException("没有找到摄像头设备。");
                }

                // 默认选择第一个摄像头
                videoDevice = new VideoCaptureDevice(videoDevices[0].MonikerString);
                videoDevice.NewFrame += videoDevice_NewFrame;
            }
            catch (Exception ex)
            {
                MessageBox.Show("初始化摄像头失败: " + ex.Message);
            }
        }
        private void videoDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            // 确保pictureBoxMonitor已经初始化
            if (pictureBox1.InvokeRequired)
            {
                pictureBox1.Invoke(new Action(() =>
                {
                    // 释放旧的图像
                    if (pictureBox1.Image != null)
                    {
                        pictureBox1.Image.Dispose();
                    }
                    // 设置新的图像
                    pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
                }));
            }
            else
            {
                // 释放旧的图像
                if (pictureBox1.Image != null)
                {
                    pictureBox1.Image.Dispose();
                }
                // 设置新的图像
                pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
            }
        }


        private void VedioForm_Load(object sender, EventArgs e)
        {
            if (videoDevice != null)
            {
                videoDevice.Start();
                lblStatus.Text = "监控状态：正在运行";
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (videoDevice != null)
            {
                videoDevice.Start();
                lblStatus.Text = "监控状态：正在运行";
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (videoDevice != null && videoDevice.IsRunning)
            {
                videoDevice.SignalToStop();
                lblStatus.Text = "监控状态：已停止";
            }
        }
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            if (videoDevice != null && videoDevice.IsRunning)
            {
                videoDevice.SignalToStop();
            }
        }
    }
}

